package com.promote.hotspot.sample.controller;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.promote.hotspot.client.ClientContext;
import org.promote.hotspot.client.collector.HotCollector;
import org.promote.hotspot.client.collector.HotCollectorFactory;
import org.promote.hotspot.client.filter.HotSpotDetector;
import org.promote.hotspot.client.model.KeyHotModel;
import org.promote.hotspot.client.pusher.HotPusherFactory;
import org.promote.hotspot.common.model.HotKeyModel;
import org.promote.hotspot.common.model.KeyCountModel;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author enping.jep
 * @date 2023/11/28 21:06
 **/


@RestController
public class TestController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "hello";
    }

    @RequestMapping("/hotKey")
    public Object hotKey(String key) {
        if (!StringUtils.isEmpty(key) && HotSpotDetector.isHotKey(key)) {
            return "isHot";
        } else {
            return "noHot";
        }
    }

    @RequestMapping("/pushKeys")
    public String pushKeys() {
        HotCollector<HotKeyModel, HotKeyModel> collectHK = HotCollectorFactory.getHotKeyCollector();
        List<HotKeyModel> hotKeyModels = collectHK.lockAndGetResult();
        if (CollectionUtils.isNotEmpty(hotKeyModels)) {
//            log.info("发送hotKeyModels--" + hotKeyModels);
            HotPusherFactory.getHotPusher().sendHotKey(ClientContext.APP_NAME, hotKeyModels);
        }
        return "success";
    }

    @RequestMapping("/pushSummary")
    public String pushSummary() {
        HotCollector<KeyHotModel, KeyCountModel> collectHK = HotCollectorFactory.getHotCountColletor();
        List<KeyCountModel> keyCountModels = collectHK.lockAndGetResult();
        if (CollectionUtils.isNotEmpty(keyCountModels)) {
//            log.info("发送keyCountModels--" + keyCountModels);
            HotPusherFactory.getHotPusher().sendHotCount(ClientContext.APP_NAME, keyCountModels);
        }
        return "success";
    }
}
